home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / CUGUK / PROG_TOO / C023A.ZIP / PART2 / ZOPT3.C < prev    next >
Text File  |  1990-01-31  |  4KB  |  179 lines

  1. /*
  2.  * zopt - five pass optimiser for small-C  (third pass)
  3.  *        v2.0 - uses independent processes
  4.  *
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include "zopt.h"
  10.  
  11. pass3()
  12. {
  13.     int i, saved[9] ;
  14.     char *temp, *tail ;
  15.     char *last, *this, *next ;
  16.  
  17.     /* start pass 4 */
  18.     switch_down(1) ;
  19.  
  20.     saved[0] = saved[1] = saved[2] = saved[3] = saved[4] = 0 ;
  21.     saved[5] = saved[6] = saved[7] = saved[8] = i = 0 ;
  22.  
  23.     last = alloc(LINELEN) ;
  24.     this = alloc(LINELEN) ;
  25.     next = alloc(LINELEN) ;
  26.  
  27.     p_read(last) ;
  28.     p_read(this) ;
  29.  
  30.     while ( p_read(next) ) {
  31.  
  32.         /* arithmetic shifts by constants */
  33.         if ( strcmp(Exdehl,last) == 0 ) {
  34.           if ( tail=match(Ldhl,this) ) {
  35.             if ( strcmp("\tCALL ccasl", next) == 0 ) {
  36.               if ( strlen(tail) == 1 && allnum(tail) ) {
  37.                 switch ( *tail ) {
  38.                     case '7' :    c_write(Addhlhl) ;
  39.                                 --saved[1] ;
  40.                     case '6' :    c_write(Addhlhl) ;
  41.                                 --saved[1] ;
  42.                     case '5' :    c_write(Addhlhl) ;
  43.                                 --saved[1] ;
  44.                     case '4' :    c_write(Addhlhl) ;
  45.                                 --saved[1] ;
  46.                     case '3' :    c_write(Addhlhl) ;
  47.                                 --saved[1] ;
  48.                     case '2' :    c_write(Addhlhl) ;
  49.                                 --saved[1] ;
  50.                     case '1' :    strcpy(last,Addhlhl) ;
  51.                                 saved[1] += 6 ;
  52.                                 ++i ;
  53.                                 p_read(this) ;
  54.                                 p_read(next) ;
  55.                                 break ;
  56.                     case '9' :    c_write(Addhlhl) ;
  57.                                 --saved[1] ;
  58.                     case '8' :    strcpy(last,"\tLD H,L") ;
  59.                                 strcpy(this,"\tLD L,0") ;
  60.                                 p_read(next) ;
  61.                                 ++i ;
  62.                                 saved[1] += 4 ;
  63.                                 break ;
  64.                     case '0' :    p_read(last) ;
  65.                                 p_read(this) ;
  66.                                 p_read(next) ;
  67.                                 saved[1] += 7 ;
  68.                                 ++i ;
  69.                                 break ;
  70.                 }
  71.               }
  72.             }
  73.             else if (strcmp("\tCALL ccasr",next) == 0 &&
  74.                          strcmp("8",tail) == 0 ) {
  75.                 strcpy(last,"\tLD A,H") ;
  76.                 strcpy(this,"\tCALL ccsxt") ;
  77.                 p_read(next) ;
  78.                 saved[1] += 3 ;
  79.                 ++i ;
  80.             }
  81.           }
  82.         }
  83.  
  84.         /* double EX DE,HL */
  85.         if ( strcmp( Exdehl,last) == 0 ) {
  86.           if ( strcmp( Exdehl,this) == 0 ) {
  87.             temp = last ;
  88.             last = next ;
  89.             next = temp ;
  90.             p_read( this ) ;
  91.             p_read( next ) ;
  92.             ++saved[0] ;
  93.           }
  94.         }
  95.  
  96.  
  97.         /* ccgint to DE expanded */
  98.         if ( strcmp("\tCALL ccgint",last) == 0 ) {
  99.           if ( strcmp(Exdehl,this) == 0 ) {
  100.             if ( match(Ldhl,next) |
  101.                  strcmp(Pophl,next) == 0 ) {
  102.             c_write( "\tLD E,(HL)" ) ;
  103.             strcpy( last, Inchl ) ;
  104.             strcpy( this, "\tLD D,(HL)" ) ;
  105.             ++saved[3] ;
  106.             }
  107.           }
  108.         }
  109.  
  110.         /* skip in-line expansions if compact code wanted */
  111.         if ( Compact == 0 ) {
  112.  
  113.             /* expand ccgint */
  114.             if( strcmp("\tCALL ccgint", last) == 0 ) {
  115.                 c_write( "\tLD A,(HL)" ) ;
  116.                 c_write( Inchl ) ;
  117.                 c_write( "\tLD H,(HL)" ) ;
  118.                 strcpy( last, "\tLD L,A" ) ;
  119.                 --saved[4] ;
  120.             }
  121.  
  122.             /* expand ccgchar to DE */
  123.             if ( strcmp("\tCALL ccgchar",last) == 0 ) {
  124.               if ( strcmp(Exdehl,this) == 0 ) {
  125.                 if ( match(Ldhl,next) ||
  126.                      strcmp(Pophl,next) == 0 ) {
  127.                 c_write( "\tLD A,(HL)" ) ;
  128.                 c_write( "\tLD E,A" ) ;
  129.                 c_write( "\tRLCA" ) ;
  130.                 strcpy( last, "\tSBC A,A" ) ;
  131.                 strcpy( this, "\tLD D,A" ) ;
  132.                 --saved[6] ;
  133.                 }
  134.               }
  135.             }
  136.  
  137.             /* expand ccgchar */
  138.             if ( strcmp("\tCALL ccgchar",last) == 0 ) {
  139.                 c_write( "\tLD A,(HL)" ) ;
  140.                 c_write( "\tLD L,A" ) ;
  141.                 c_write( "\tRLCA" ) ;
  142.                 c_write( "\tSBC A,A" ) ;
  143.                 strcpy( last, "\tLD H,A" ) ;
  144.                 --saved[7] ;
  145.             }
  146.         }
  147.  
  148.         c_write( last ) ;
  149.         temp = last ;
  150.         last = this ;
  151.         this = next ;
  152.         next = temp ;
  153.         if (cpm(CONIN, 255) == CTRLC) exit() ;
  154.     }
  155.     c_write( last ) ;
  156.     c_write( this ) ;
  157.  
  158.     puts("Double EX DE,HL:              "); putdec(saved[0]) ;
  159.     putchar('\n') ;
  160.     puts("Expand ccgint to DE:          "); putdec(saved[3]) ;
  161.     putchar('\n') ;
  162.     if ( Compact == FALSE ) {
  163.         puts("Expand ccgint:                "); putdec(saved[4]) ;
  164.         putchar('\n') ;
  165.         puts("Expand ccgchar to DE:         "); putdec(saved[6]) ;
  166.         putchar('\n') ;
  167.         puts("Expand ccgchar:               "); putdec(saved[7]) ;
  168.         putchar('\n') ;
  169.     }
  170.     puts("Shift by constant:            "); putdec(i) ;
  171.     putchar('\n') ;
  172.     putchar('\n') ;
  173.     i = saved[0]*2 + saved[1] + saved[2] + saved[3] + saved[4] ;
  174.     i += saved[6] + saved[7]*2 + saved[8] ;
  175.     pr_total(i);
  176.  
  177.     Total += i ;
  178. }
  179.